__author__ = 'thanatos'

class Dict(dict):
    """
    Simple dict but support access as x.y style.
    """
    def __init__(self, names=(), values=(), **kw):
        super(Dict, self).__init__(**kw)
        for k, v in zip(names, values):
            self[k] = v

    def __getattr__(self, key):
        try:
            return self[key]
        except KeyError:
            return None

    def __setattr__(self, key, value):
        self[key] = value

_PAGE_NAVIGATE = 5

class Page(Dict):
    """
    a common page domain
    """
    def __init__(self, current, page_size, records, data):
        super().__init__()
        self.current = current
        self.page_size = page_size
        self.records = records
        self.data = data
        self.page_count = (records + page_size - 1) // page_size
        if self.page_count <= _PAGE_NAVIGATE:
            self.begin_index = 1
            self.end_index = self.page_count
        elif current <= _PAGE_NAVIGATE // 2:
            self.begin_index = 1
            self.end_index = _PAGE_NAVIGATE
        elif current + _PAGE_NAVIGATE // 2 > self.page_count:
            self.begin_index = self.page_count - _PAGE_NAVIGATE + 1
            self.end_index = self.page_count
        else:
            self.begin_index = current - _PAGE_NAVIGATE // 2
            self.end_index = current - _PAGE_NAVIGATE // 2
